// pages/certification/certification.js
const url = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioCheckVal: 0,
    baseUrl: '',
    showImage:true,
    showImagetwo:true,

    imageUrl: '',
    prirealname:'',
    priidcard:'',
    priphone:'',
    imgid:0,

    imageUrltwo: '',
    busrealname: '',
    busidcard: '',
    busphone: '',
    imgidtwo: 0
  },
  //提交商家认证
  submitcerbus: function () {
    wx.showLoading({
      title: '请稍等',
    })
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/business-certification', // 仅为示例
      data: {
        img: this.data.imgidtwo,
        id_card: this.data.busidcard,
        real_name: this.data.busrealname,
        phone: this.data.busphone,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      success(res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.status == 0) {
          wx.showToast({
            title: '提交成功',
          })
        }
      }
    })
  },
  //提交个人认证
  submitcer:function(){
    wx.showLoading({
      title: '请稍等',
    })
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/personal-certification', // 仅为示例
      data: {
        img:this.data.imgid,
        id_card: this.data.priidcard,
        real_name: this.data.prirealname,
        phone: this.data.priphone,
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      success(res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.status == 0){
          wx.showToast({
            title: '提交成功',
          })
        }
      }
    })
  },
  prirealname:function(e){
    // console.log(e.detail.value)
    this.setData({
      prirealname:e.detail.value
    })
  },
  priidcard:function(e){
    // console.log(e.detail.value)
    this.setData({
      priidcard: e.detail.value
    })
  },
  priphone:function(e){
    // console.log(e.detail.value)
    this.setData({
      priphone: e.detail.value
    })
  },
  busrealname: function (e) {
    // console.log(e.detail.value)
    this.setData({
      busrealname: e.detail.value
    })
  },
  busidcard: function (e) {
    // console.log(e.detail.value)
    this.setData({
      busidcard: e.detail.value
    })
  },
  busphone: function (e) {
    // console.log(e.detail.value)
    this.setData({
      busphone: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: url.baseUrl
    })
    //个人认证信息
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/personal-certification', // 仅为示例，并非真实的接口地址
      data: {
      },
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      success: (res) =>{
        console.log(res,'个人')
        if (res.data != '' && res.data != null) {
          if (res.data.status == 10) {
            var user = wx.getStorageSync('user')
            user.personal_cer = 1
            wx.setStorageSync('user', user)
          }

          this.setData({
            imageUrl: res.data.img,
            prirealname: res.data.real_name,
            priidcard: res.data.id_card,
            priphone: res.data.phone,
          })
        }
        
       
      }
    })
   //商家认证
    wx.request({
      url: 'https://api.tianlala.com.cn/business-certification', // 仅为示例，并非真实的接口地址
      data: {
      },
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      success:(res)=>{
        console.log(res, '商家')
        if (res.data != '' && res.data != null){
          if (res.data.status == 10) {
            var user = wx.getStorageSync('user')
            user.business_cer = 1
            wx.setStorageSync('user', user)
          }
          
          this.setData({
            imageUrltwo: res.data.img,
            busrealname: res.data.real_name,
            busidcard: res.data.id_card,
            busphone: res.data.phone,
          })
        }

      }
    })
    
  },
  radioCheckedChange: function (e) {
    this.setData({
      radioCheckVal: e.detail.value
    })
  },
  //个人上传
  uploadImage(){
    wx.chooseImage({
      success: (res) => {
        let m = !this.data.showImage;
        var tempFilePaths = res.tempFilePaths  //图片

        var accesstoken = wx.getStorageSync('accesstoken')
        wx.uploadFile({
          url: 'https://api.tianlala.com.cn/images', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          header: {
            'Authorization': accesstoken
          },
          name: 'image', //文件对应的参数名字(key)
          formData: {},  //其它的表单信息
          success: (res) => {
            console.log(res)
            var info = JSON.parse(res.data)
            console.log(info.id, info.image)
            this.setData({
              imageUrl: info.image,
              // showImage: m,
              imgid: info.id
            })

          }
        })
      }
    })
  },

  //商家上传
  imageupload(){
    wx.chooseImage({
      success: (res) => {
        let m = !this.data.showImage;
        var tempFilePaths = res.tempFilePaths  //图片

        var accesstoken = wx.getStorageSync('accesstoken')
        wx.uploadFile({
          url: 'https://api.tianlala.com.cn/images', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          header: {
            'Authorization': accesstoken
          },
          name: 'image', //文件对应的参数名字(key)
          formData: {},  //其它的表单信息
          success: (res) => {
            console.log(res)
            var info = JSON.parse(res.data)
            console.log(info.id, info.image)
            this.setData({
              imageUrltwo: info.image,
              // showImagetwo: m,
              imgidtwo: info.id

            })

          }
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})